<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/doc/src/examples/tablet.qdoc -->
<head>
  <title>Qt 4.3: Tablet Example</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">Tablet Example<br /><small></small></h1>
<p>Files:</p>
<ul>
<li><a href="widgets-tablet-mainwindow-cpp.html">widgets/tablet/mainwindow.cpp</a></li>
<li><a href="widgets-tablet-mainwindow-h.html">widgets/tablet/mainwindow.h</a></li>
<li><a href="widgets-tablet-tabletapplication-cpp.html">widgets/tablet/tabletapplication.cpp</a></li>
<li><a href="widgets-tablet-tabletapplication-h.html">widgets/tablet/tabletapplication.h</a></li>
<li><a href="widgets-tablet-tabletcanvas-cpp.html">widgets/tablet/tabletcanvas.cpp</a></li>
<li><a href="widgets-tablet-tabletcanvas-h.html">widgets/tablet/tabletcanvas.h</a></li>
<li><a href="widgets-tablet-main-cpp.html">widgets/tablet/main.cpp</a></li>
</ul>
<p>This example shows how to use a Wacom tablet in Qt applications.</p>
<p align="center"><img src="images/tabletexample.png" /></p><p>When you use a tablet with Qt applications, <a href="qtabletevent.html">QTabletEvent</a>s are genarated. You need to reimplement the <a href="qwidget.html#tabletEvent">tabletEvent()</a> event handler if you want to handle tablet events. Events are generated when the device used for drawing enters and leaves the proximity of the tablet (i.e&#x2e;, when it is close but not pressed down on it), when a device is pushed down and released from it, and when a device is moved on the tablet.</p>
<p>The information available in <a href="qtabletevent.html">QTabletEvent</a> depends on the device used. The tablet in this example has two different devices for drawing: a stylus and an airbrush. For both devices the event contains the position of the device, pressure on the tablet, vertical tilt, and horizontal tilt (i.e, the angle between the device and the perpendicular of the tablet). The airbrush has a finger wheel; the position of this is also available in the tablet event.</p>
<p>In this example we implement a drawing program. You can use the stylus to draw on the tablet as you use a pencil on paper. When you draw with the airbrush you get a spray of paint; the finger wheel is used to change the density of the spray. The pressure and tilt can change the alpha and saturation values of the <a href="qcolor.html">QColor</a> and the width of the <a href="qpen.html">QPen</a> used for drawing.</p>
<p>The example consists of the following:</p>
<ul>
<li>The <tt>MainWindow</tt> class inherits <a href="qmainwindow.html">QMainWindow</a> and creates the examples menus and connect their slots and signals.</li>
<li>The <tt>TabletCanvas</tt> class inherits <a href="qwidget.html">QWidget</a> and receives tablet events. It uses the events to paint on a <a href="qimage.html">QImage</a>, which it draws onto itself.</li>
<li>The <tt>TabletApplication</tt> class inherits <a href="qapplication.html">QApplication</a>. This class handles tablet events that are not sent to <tt>tabletEvent()</tt>. We will look at this later.</li>
<li>The <tt>main()</tt> function creates a <tt>MainWindow</tt> and shows it as a top level window.</li>
</ul>
<a name="mainwindow-class-definition"></a>
<h2>MainWindow Class Definition</h2>
<p>The <tt>MainWindow</tt> creates a <tt>TabletCanvas</tt> and sets it as its center widget.</p>
<pre> class MainWindow : public QMainWindow
 {
     Q_OBJECT

 public:
     MainWindow(TabletCanvas *canvas);

 private slots:
     void brushColorAct();
     void alphaActionTriggered(QAction *action);
     void lineWidthActionTriggered(QAction *action);
     void saturationActionTriggered(QAction *action);
     void saveAct();
     void loadAct();
     void aboutAct();

 private:
     void createActions();
     void createMenus();

     TabletCanvas *myCanvas;

     QAction *brushColorAction;
     QActionGroup *brushActionGroup;

     QActionGroup *alphaChannelGroup;
     QAction *alphaChannelPressureAction;
     QAction *alphaChannelTiltAction;
     QAction *noAlphaChannelAction;

     QActionGroup *colorSaturationGroup;
     QAction *colorSaturationVTiltAction;
     QAction *colorSaturationHTiltAction;
     QAction *colorSaturationPressureAction;
     QAction *noColorSaturationAction;

     QActionGroup *lineWidthGroup;
     QAction *lineWidthPressureAction;
     QAction *lineWidthTiltAction;
     QAction *lineWidthFixedAction;

     QAction *exitAction;
     QAction *saveAction;
     QAction *loadAction;

     QAction *aboutAction;
     QAction *aboutQtAction;

     QMenu *fileMenu;
     QMenu *brushMenu;
     QMenu *tabletMenu;
     QMenu *helpMenu;
     QMenu *colorSaturationMenu;
     QMenu *lineWidthMenu;
     QMenu *alphaChannelMenu;
 };</pre>
<p>The QActions let the user select if the tablets pressure and tilt should change the pen width, color alpha component and color saturation. <tt>createActions()</tt> creates all actions, and <tt>createMenus()</tt> sets up the menus with the actions. We have one <a href="qactiongroup.html">QActionGroup</a> for the actions that alter the alpha channel, color saturation and line width respectively. The action groups are connected to the <tt>alphaActionTriggered()</tt>, <tt>colorSaturationActiontriggered()</tt>, and <tt>lineWidthActionTriggered()</tt> slots, which calls functions in <tt>myCanvas</tt>.</p>
<a name="mainwindow-class-implementation"></a>
<h2>MainWindow Class Implementation</h2>
<p>We start width a look at the constructor <tt>MainWindow()</tt>:</p>
<pre> MainWindow::MainWindow(TabletCanvas *canvas)
 {
     myCanvas = canvas;
     createActions();
     createMenus();

     myCanvas-&gt;setColor(Qt::red);
     myCanvas-&gt;setLineWidthType(TabletCanvas::LineWidthPressure);
     myCanvas-&gt;setAlphaChannelType(TabletCanvas::NoAlpha);
     myCanvas-&gt;setColorSaturationType(TabletCanvas::NoSaturation);

     setWindowTitle(tr(&quot;Tablet Example&quot;));
     setCentralWidget(myCanvas);
 }</pre>
<p>In the constructor we create the canvas, actions, and menus. We set the canvas as the center widget. We also initialize the canvas to match the state of our menus and start drawing with a red color.</p>
<p>Here is the implementation of <tt>brushColorAct()</tt>:</p>
<pre> void MainWindow::brushColorAct()
 {
     QColor color = QColorDialog::getColor(myCanvas-&gt;color());

     if (color.isValid())
         myCanvas-&gt;setColor(color);
 }</pre>
<p>We let the user pick a color with a <a href="qcolordialog.html">QColorDialog</a>. If it is valid, we set a new drawing color with <tt>setColor()</tt>.</p>
<p>Here is the implementation of <tt>alphaActionTriggered()</tt>:</p>
<pre> void MainWindow::alphaActionTriggered(QAction *action)
 {
     if (action == alphaChannelPressureAction) {
         myCanvas-&gt;setAlphaChannelType(TabletCanvas::AlphaPressure);
     } else if (action == alphaChannelTiltAction) {
         myCanvas-&gt;setAlphaChannelType(TabletCanvas::AlphaTilt);
     } else {
         myCanvas-&gt;setAlphaChannelType(TabletCanvas::NoAlpha);
     }
 }</pre>
<p>The <tt>TabletCanvas</tt> class supports two ways by which the alpha channel of the drawing color can be changed: tablet pressure and tilt. We have one action for each and an action if the alpha channel should not be changed.</p>
<p>Here is the implementation of <tt>lineWidthActionTriggered()</tt>:</p>
<pre> void MainWindow::lineWidthActionTriggered(QAction *action)
 {
     if (action == lineWidthPressureAction) {
         myCanvas-&gt;setLineWidthType(TabletCanvas::LineWidthPressure);
     } else if (action == lineWidthTiltAction) {
         myCanvas-&gt;setLineWidthType(TabletCanvas::LineWidthTilt);
     } else {
         myCanvas-&gt;setLineWidthType(TabletCanvas::NoLineWidth);
     }
 }</pre>
<p>We check which action is selected in <tt>lineWidthGroup</tt>, and set how the canvas should change the drawing line width.</p>
<p>Here is the implementation of <tt>saturationActionTriggered()</tt>:</p>
<pre> void MainWindow::saturationActionTriggered(QAction *action)
 {
     if (action == colorSaturationVTiltAction) {
         myCanvas-&gt;setColorSaturationType(TabletCanvas::SaturationVTilt);
     } else if (action == colorSaturationHTiltAction) {
         myCanvas-&gt;setColorSaturationType(TabletCanvas::SaturationHTilt);
     } else if (action == colorSaturationPressureAction) {
         myCanvas-&gt;setColorSaturationType(TabletCanvas::SaturationPressure);
     } else {
         myCanvas-&gt;setColorSaturationType(TabletCanvas::NoSaturation);
     }
 }</pre>
<p>We check which action is selected in <tt>colorSaturationGroup</tt>, and set how the canvas should change the color saturation of the drawing color.</p>
<p>Here is the implementation of <tt>saveAct()</tt>:</p>
<pre> void MainWindow::saveAct()
 {
     QString path = QDir::currentPath() + &quot;/untitled.png&quot;;
     QString fileName = QFileDialog::getSaveFileName(this, tr(&quot;Save Picture&quot;),
                              path);

     if (!myCanvas-&gt;saveImage(fileName))
         QMessageBox::information(this, &quot;Error Saving Picture&quot;,
                                  &quot;Could not save the image&quot;);
 }</pre>
<p>We use the <a href="qfiledialog.html">QFileDialog</a> to let the user select a file to save the drawing in. It is the <tt>TabletCanvas</tt> that save the drawing, so we call its <tt>saveImage()</tt> function.</p>
<p>Here is the implementation of <tt>loadAct()</tt>:</p>
<pre> void MainWindow::loadAct()
 {
     QString fileName = QFileDialog::getOpenFileName(this, tr(&quot;Open Picture&quot;),
                                                     QDir::currentPath());

     if (!myCanvas-&gt;loadImage(fileName))
         QMessageBox::information(this, &quot;Error Opening Picture&quot;,
                                  &quot;Could not open picture&quot;);
 }</pre>
<p>We let the user select the image file to be opened with a <a href="qfiledialog.html">QFileDialog</a>; we then ask the canvas to load the image with <tt>loadImage()</tt>.</p>
<p>Here is the implementation of <tt>aboutAct()</tt>:</p>
<pre> void MainWindow::aboutAct()
 {
     QMessageBox::about(this, tr(&quot;About Tablet Example&quot;),
                        tr(&quot;This example shows use of a Wacom tablet in Qt&quot;));
 }</pre>
<p>We show a message box with a short description of the example.</p>
<p><tt>createActions()</tt> creates all actions and action groups of the example. We look at the creation of one action group and its actions. See the <a href="mainwindows-application.html">application example</a> if you want a high-level introduction to QActions.</p>
<p>Here is the implementation of <tt>createActions</tt>:</p>
<pre> void MainWindow::createActions()
 {
     ...
     alphaChannelPressureAction = new QAction(tr(&quot;&amp;Pressure&quot;), this);
     alphaChannelPressureAction-&gt;setCheckable(true);

     alphaChannelTiltAction = new QAction(tr(&quot;&amp;Tilt&quot;), this);
     alphaChannelTiltAction-&gt;setCheckable(true);

     noAlphaChannelAction = new QAction(tr(&quot;No Alpha Channel&quot;), this);
     noAlphaChannelAction-&gt;setCheckable(true);
     noAlphaChannelAction-&gt;setChecked(true);

     alphaChannelGroup = new QActionGroup(this);
     alphaChannelGroup-&gt;addAction(alphaChannelPressureAction);
     alphaChannelGroup-&gt;addAction(alphaChannelTiltAction);
     alphaChannelGroup-&gt;addAction(noAlphaChannelAction);
     connect(alphaChannelGroup, SIGNAL(triggered(QAction *)),
             this, SLOT(alphaActionTriggered(QAction *)));</pre>
<p>We want the user to be able to choose if the drawing color's alpha component should be changed by the tablet pressure or tilt. We have one action for each choice and an action if the alpha channel is not to be changed, i.e, the color is opaque. We make the actions checkable; the <tt>alphaChannelGroup</tt> will then ensure that only one of the actions are checked at any time. The <tt>triggered()</tt> signal is emitted when an action is checked.</p>
<pre>     ...
 }</pre>
<p>Here is the implementation of <tt>createMenus()</tt>:</p>
<pre> void MainWindow::createMenus()
 {
     fileMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;File&quot;));
     fileMenu-&gt;addAction(loadAction);
     fileMenu-&gt;addAction(saveAction);
     fileMenu-&gt;addSeparator();
     fileMenu-&gt;addAction(exitAction);

     brushMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;Brush&quot;));
     brushMenu-&gt;addAction(brushColorAction);

     tabletMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;Tablet&quot;));

     lineWidthMenu = tabletMenu-&gt;addMenu(tr(&quot;&amp;Line Width&quot;));
     lineWidthMenu-&gt;addAction(lineWidthPressureAction);
     lineWidthMenu-&gt;addAction(lineWidthTiltAction);
     lineWidthMenu-&gt;addAction(lineWidthFixedAction);

     alphaChannelMenu = tabletMenu-&gt;addMenu(tr(&quot;&amp;Alpha Channel&quot;));
     alphaChannelMenu-&gt;addAction(alphaChannelPressureAction);
     alphaChannelMenu-&gt;addAction(alphaChannelTiltAction);
     alphaChannelMenu-&gt;addAction(noAlphaChannelAction);

     colorSaturationMenu = tabletMenu-&gt;addMenu(tr(&quot;&amp;Color Saturation&quot;));
     colorSaturationMenu-&gt;addAction(colorSaturationVTiltAction);
     colorSaturationMenu-&gt;addAction(colorSaturationHTiltAction);
     colorSaturationMenu-&gt;addAction(noColorSaturationAction);

     helpMenu = menuBar()-&gt;addMenu(&quot;&amp;Help&quot;);
     helpMenu-&gt;addAction(aboutAction);
     helpMenu-&gt;addAction(aboutQtAction);
 }</pre>
<p>We create the menus of the example and add the actions to them.</p>
<a name="tabletcanvas-class-definition"></a>
<h2>TabletCanvas Class Definition</h2>
<p>The <tt>TabletCanvas</tt> class provides a surface on which the user can draw with a tablet.</p>
<pre> class TabletCanvas : public QWidget
 {
     Q_OBJECT

 public:
     enum AlphaChannelType { AlphaPressure, AlphaTilt, NoAlpha };
     enum ColorSaturationType { SaturationVTilt, SaturationHTilt,
                                SaturationPressure, NoSaturation };
     enum LineWidthType { LineWidthPressure, LineWidthTilt, NoLineWidth };

     TabletCanvas();

     bool saveImage(const QString &amp;file);
     bool loadImage(const QString &amp;file);
     void setAlphaChannelType(AlphaChannelType type)
         { alphaChannelType = type; }
     void setColorSaturationType(ColorSaturationType type)
         { colorSaturationType = type; }
     void setLineWidthType(LineWidthType type)
         { lineWidthType = type; }
     void setColor(const QColor &amp;color)
         { myColor = color; }
     QColor color() const
         { return myColor; }
     void setTabletDevice(QTabletEvent::TabletDevice device)
         { myTabletDevice = device; }
     int max(int a, int b)
         { return a &gt; b ? a : b; }

 protected:
     void tabletEvent(QTabletEvent *event);
     void paintEvent(QPaintEvent *event);

 private:
     void paintImage(QPainter &amp;painter, QTabletEvent *event);
     Qt::BrushStyle brushPattern(qreal value);
     void updateBrush(QTabletEvent *event);

     AlphaChannelType alphaChannelType;
     ColorSaturationType colorSaturationType;
     LineWidthType lineWidthType;
     QTabletEvent::PointerType pointerType;
     QTabletEvent::TabletDevice myTabletDevice;
     QColor myColor;

     QImage image;
     QBrush myBrush;
     QPen myPen;
     bool deviceDown;
     QPoint polyLine[3];
 };</pre>
<p>The canvas can change the alpha channel, color saturation, and line width of the drawing. We have one enum for each of these; their values decide if it is the tablet pressure or tilt that will alter them. We keep a private variable for each, the <tt>alphaChannelType</tt>, <tt>colorSturationType</tt>, and <tt>penWidthType</tt>, which we provide access functions for.</p>
<p>We draw on a <a href="qimage.html">QImage</a> with <tt>myPen</tt> and <tt>myBrush</tt> using <tt>myColor</tt>. The <tt>saveImage()</tt> and <tt>loadImage()</tt> saves and loads the <a href="qimage.html">QImage</a> to disk. The image is drawn on the widget in <tt>paintEvent()</tt>. The <tt>pointerType</tt> and <tt>deviceType</tt> keeps the type of pointer, which is either a pen or an eraser, and device currently used on the tablet, which is either a stylus or an airbrush.</p>
<p>The interpretation of events from the tablet is done in <tt>tabletEvent()</tt>; <tt>paintImage()</tt>, <tt>updateBrush()</tt>, and <tt>brushPattern()</tt> are helper functions used by <tt>tabletEvent()</tt>.</p>
<a name="tabletcanvas-class-implementation"></a>
<h2>TabletCanvas Class Implementation</h2>
<p>We start with a look at the constructor:</p>
<pre> TabletCanvas::TabletCanvas()
 {
     myBrush = QBrush();
     myPen = QPen();
     image = QImage(500, 500, QImage::Format_ARGB32);
     QPainter painter(&amp;image);
     painter.fillRect(0, 0, 499, 499, Qt::white);
     setAutoFillBackground(true);
     deviceDown = false;
     myColor = Qt::red;
     myTabletDevice = QTabletEvent::Stylus;
     alphaChannelType = NoAlpha;
     colorSaturationType = NoSaturation;
     lineWidthType = LineWidthPressure;
 }</pre>
<p>In the constructor we initialize our class variables. We need to draw the background of our image, as the default is gray.</p>
<p>Here is the implementation of <tt>saveImage()</tt>:</p>
<pre> bool TabletCanvas::saveImage(const QString &amp;file)
 {
     return image.save(file);
 }</pre>
<p><a href="qimage.html">QImage</a> implements functionality to save itself to disk, so we simply call <a href="qimage.html#save">save()</a>.</p>
<p>Here is the implementation of <tt>loadImage()</tt>:</p>
<pre> bool TabletCanvas::loadImage(const QString &amp;file)
 {
     bool success = image.load(file);

     if (success) {
         update();
         return true;
     }
     return false;
 }</pre>
<p>We simply call <a href="qimage.html#load">load()</a>, which loads the image in <i>file</i>.</p>
<p>Here is the implementation of <tt>tabletEvent()</tt>:</p>
<pre> void TabletCanvas::tabletEvent(QTabletEvent *event)
 {

     switch (event-&gt;type()) {
         case QEvent::TabletPress:
             if (!deviceDown)
                 deviceDown = true;
             break;
         case QEvent::TabletRelease:
             if (deviceDown)
                 deviceDown = false;
             break;
         case QEvent::TabletMove:
             polyLine[2] = polyLine[1];
             polyLine[1] = polyLine[0];
             polyLine[0] = event-&gt;pos();

             if (deviceDown) {
                 updateBrush(event);
                 QPainter painter(&amp;image);
                 paintImage(painter, event);
             }
             break;
         default:
             break;
     }
     update();
 }</pre>
<p>We get three kind of events to this function: TabletPress, TabletRelease, and TabletMove, which is generated when a device is pressed down on, leaves, or moves on the tablet. We set the <tt>deviceDown</tt> to true when a device is pressed down on the tablet; we then know when we should draw when we receive move events. We have implemented the <tt>updateBrush()</tt> and <tt>paintImage()</tt> helper functions to update <tt>myBrush</tt> and <tt>myPen</tt> after the state of <tt>alphaChannelType</tt>, <tt>colorSaturationType</tt>, and <tt>lineWidthType</tt>.</p>
<p>Here is the implementation of <tt>paintEvent()</tt>:</p>
<pre> void TabletCanvas::paintEvent(QPaintEvent *)
 {
     QPainter painter(this);
     painter.drawImage(QPoint(0, 0), image);
 }</pre>
<p>We simply draw the image to the top left of the widget.</p>
<p>Here is the implementation of <tt>paintImage()</tt>:</p>
<pre> void TabletCanvas::paintImage(QPainter &amp;painter, QTabletEvent *event)
 {
     QPoint brushAdjust(10, 10);

     switch (myTabletDevice) {
         case QTabletEvent::Stylus:
             painter.setBrush(myBrush);
             painter.setPen(myPen);
             painter.drawLine(polyLine[1], event-&gt;pos());
             break;
         case QTabletEvent::Airbrush:
             myBrush.setColor(myColor);
             myBrush.setStyle(brushPattern(event-&gt;pressure()));
             painter.setPen(Qt::NoPen);
             painter.setBrush(myBrush);

             for (int i = 0; i &lt; 3; ++i) {
                 painter.drawEllipse(QRect(polyLine[i] - brushAdjust,
                                     polyLine[i] + brushAdjust));
             }
             break;
         default:
             qWarning(&quot;Unsupported tablet device.&quot;);
     }
 }</pre>
<p>In this function we draw on the image based on the movement of the device. If the device used on the tablet is a stylus we want to draw a line between the positions of the stylus recorded in <tt>polyLine</tt>. If it is an airbrush we want to draw a circle of points with a point density based on the tangential pressure, which is the position of the finger wheel on the airbrush. We use the <a href="qt.html#BrushStyle-enum">Qt::BrushStyle</a> to draw the points as it has styles that draw points with different density; we select the style based on the tangential pressure in <tt>brushPattern()</tt>.</p>
<pre> Qt::BrushStyle TabletCanvas::brushPattern(qreal value)
 {
     int pattern = int((value) * 100.0) % 7;

     switch (pattern) {
         case 0:
             return Qt::SolidPattern;
         case 1:
             return Qt::Dense1Pattern;
         case 2:
             return Qt::Dense2Pattern;
         case 3:
             return Qt::Dense3Pattern;
         case 4:
             return Qt::Dense4Pattern;
         case 5:
             return Qt::Dense5Pattern;
         case 6:
             return Qt::Dense6Pattern;
         default:
             return Qt::Dense7Pattern;
     }
 }</pre>
<p>We return a brush style with a point density that increases with the tangential pressure.</p>
<p>In <tt>updateBrush()</tt> we set the pen and brush used for drawing to match <tt>alphaChannelType</tt>, <tt>lineWidthType</tt>, <tt>colorSaturationType</tt>, and <tt>myColor</tt>. We will examine the code to set up <tt>myBrush</tt> and <tt>myPen</tt> for each of these variables:</p>
<pre> void TabletCanvas::updateBrush(QTabletEvent *event)
 {
     int hue, saturation, value, alpha;
     myColor.getHsv(&amp;hue, &amp;saturation, &amp;value, &amp;alpha);

     int vValue = int(((event-&gt;yTilt() + 60.0) / 120.0) * 255);
     int hValue = int(((event-&gt;xTilt() + 60.0) / 120.0) * 255);</pre>
<p>We fetch the current drawingcolor's hue, saturation, value, and alpha values. <tt>hValue</tt> and <tt>vValue</tt> are set to the horizontal and vertical tilt as a number from 0 to 255. The original values are in degrees from -60 to 60, i.e&#x2e;, 0 equals -60, 127 equals 0, and 255 equals 60 degrees. The angle measured is between the device and the perpendicular of the tablet (see <a href="qtabletevent.html">QTabletEvent</a> for an illustration).</p>
<pre>     switch (alphaChannelType) {
         case AlphaPressure:
             myColor.setAlpha(int(event-&gt;pressure() * 255.0));
             break;
         case AlphaTilt:
             myColor.setAlpha(max(abs(vValue - 127), abs(hValue - 127)));
             break;
         default:
             myColor.setAlpha(255);
     }</pre>
<p>The alpha channel of <a href="qcolor.html">QColor</a> is given as a number between 0 and 255 where 0 is transparent and 255 is opaque. <a href="qtabletevent.html#pressure">pressure()</a> returns the pressure as a qreal between 0.0 and 1.0&#x2e; By subtracting 127 from the tilt values and taking the absolute value we get the smallest alpha values (i.e&#x2e;, the color is most transparent) when the pen is perpendicular to the tablet. We select the largest of the vertical and horizontal tilt value.</p>
<pre>     switch (colorSaturationType) {
         case SaturationVTilt:
             myColor.setHsv(hue, vValue, value, alpha);
             break;
         case SaturationHTilt:
             myColor.setHsv(hue, hValue, value, alpha);
             break;
         case SaturationPressure:
             myColor.setHsv(hue, int(event-&gt;pressure() * 255.0), value, alpha);
             break;
         default:
             ;
     }</pre>
<p>The colorsaturation is given as a number between 0 and 255. It is set with <a href="qcolor.html#setHsv">setHsv()</a>. We can set the tilt values directly, but must multiply the pressure to a number between 0 and 255.</p>
<pre>     switch (lineWidthType) {
         case LineWidthPressure:
             myPen.setWidthF(event-&gt;pressure() * 10 + 1);
             break;
         case LineWidthTilt:
             myPen.setWidthF(max(abs(vValue - 127), abs(hValue - 127)) / 12);
             break;
         default:
             myPen.setWidthF(1);
     }</pre>
<p>The width of the pen increases with the pressure. When the pen width is controlled with the tilt we let the width increse with the angle between the device and the perpendicular of the tablet.</p>
<pre>     if (event-&gt;pointerType() == QTabletEvent::Eraser) {
         myBrush.setColor(Qt::white);
         myPen.setColor(Qt::white);
         myPen.setWidthF(event-&gt;pressure() * 10 + 1);
     } else {
         myBrush.setColor(myColor);
         myPen.setColor(myColor);
     }
 }</pre>
<p>We finally check wether the pointer is the stylus or the eraser. If it is the eraser, we set the color to the background color of the image an let the pressure decide the pen width, else we set the colors we have set up previously in the function.</p>
<a name="tabletapplication-class-definition"></a>
<h2>TabletApplication Class Definition</h2>
<p>We inherit <a href="qapplication.html">QApplication</a> in this class because we want to reimplement the <a href="qobject.html#event">event()</a> function.</p>
<pre> class TabletApplication : public QApplication
 {
     Q_OBJECT

 public:
     TabletApplication(int &amp;argv, char **args)
     : QApplication(argv, args) {}

     bool event(QEvent *event);
     void setCanvas(TabletCanvas *canvas)
         { myCanvas = canvas; }

 private:
     TabletCanvas *myCanvas;
 };</pre>
<p>We keep a <tt>TabletCanvas</tt> we send the device type of the events we handle in the <tt>event()</tt> function to. The TabletEnterProximity and TabletLeaveProximity events are not sendt to the <a href="qapplication.html">QApplication</a> object, while other tablet events are sendt to the <a href="qwidget.html">QWidget</a>'s <tt>event()</tt>, which sends them on to <a href="qwidget.html#tabletEvent">tabletEvent()</a>. Since we want to handle these events we have implemented <tt>TabletApplication</tt>.</p>
<a name="tabletapplication-class-implementation"></a>
<h2>TabletApplication Class Implementation</h2>
<p>Here is the implementation of <tt>event()</tt>:</p>
<pre> bool TabletApplication::event(QEvent *event)
 {
     if (event-&gt;type() == QEvent::TabletEnterProximity ||
         event-&gt;type() == QEvent::TabletLeaveProximity) {
         myCanvas-&gt;setTabletDevice(
             static_cast&lt;QTabletEvent *&gt;(event)-&gt;device());
         return true;
     }
     return QApplication::event(event);
 }</pre>
<p>We use this function to handle the TabletEnterProximity and TabletLeaveProximity events, which is generated when a device enters and leaves the proximity of the tablet. The intended use of these events is to do work that is dependent on what kind of device is used on the tablet. This way, you don't have to do this work when other events are generated, which is more frequently than the leave and enter proximity events. We call <tt>setTabletDevice()</tt> in <tt>TabletCanvas</tt>.</p>
<a name="the-function"></a>
<h2>The <tt>main()</tt> function</h2>
<p>Here is the examples <tt>main()</tt> function:</p>
<pre> int main(int argv, char *args[])
 {
     TabletApplication app(argv, args);
     TabletCanvas *canvas = new TabletCanvas;
     app.setCanvas(canvas);

     MainWindow mainWindow(canvas);
     mainWindow.resize(500, 500);
     mainWindow.show();

     return app.exec();
 }</pre>
<p>In the <tt>main()</tt> function we create a <tt>MainWinow</tt> and display it as a top level window. We use the <tt>TabletApplication</tt> class. We need to set the canvas after the application is created. We cannot use classes that implement event handling before an <a href="qapplication.html">QApplication</a> object is instantiated.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
